package com.ayuku.course.ui.mentor;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.AyukuDB;
import com.ayuku.commons.Util;
import com.ayuku.commons.ui.ListWithFormMaster;
import com.ayuku.course.persistence.model.Mentor;

public class ListMentor extends ListWithFormMaster {
	private CompMentor comp;
	private Mentor pjo;

	public ListMentor(Composite parent, int style) {
		super(parent, style);
	}

	@Override
	protected String[] getColumCaption() {
		final String[] titles = { "Kode", "Nama", "Phone", "Email", "Kursus",
				"Pendidikan", "Join", "Alamat" };
		return titles;
	}

	@Override
	protected int[] getColumWidth() {
		int[] i = { 80, 200, 150, 150, 150, 150, 150, 150 };
		return i;
	}

	@Override
	protected void setFormKeyWithSelectedTableItem() {
		TableItem[] selection = getTableData().getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			setFormKey(itm.getText(0));
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN, getShell());
		}
	}

	@Override
	public void additionalProcess() {

		System.out.println("... masuk additinal proses ...");

	}

	public static List<Mentor> getList(String arg) throws Exception {
		return AyukuDB
				.getPersist()
				.readList(
						Mentor.class,
						"SELECT * FROM mentor WHERE ucase(code) LIKE ucase(?) or ucase(name) LIKE ucase(?) LIMIT 10",
						new Object[] { arg, arg });
	}

	@Override
	protected void populateTable(String sqlDml) throws Exception {
		getTableData().removeAll();
		List<Mentor> lst = getList(sqlDml);
		getTableData().setItemCount(lst.size());
		setData(new Object[lst.size()][]);

		for (int i = 0; i < lst.size(); i++) {
			Mentor pj = lst.get(i);
			getData()[i] = new String[] { Util.nullToString(pj.getCode()),
					Util.nullToString(pj.getName()),
					Util.nullToString(pj.getPhone()),
					Util.nullToString(pj.getEmail()),
					Util.nullToString(pj.getKursusdiampu()),
					Util.nullToString(pj.getEducation()),
					Util.dateToString(pj.getJoinDate()),
					Util.nullToString(pj.getAddress()) };
		}
		
		
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
		item.setText(4, Util.nullToString(obj[4]));
		item.setText(5, Util.nullToString(obj[5]));
		item.setText(6, Util.nullToString(obj[6]));
		item.setText(7, Util.nullToString(obj[7]));
	}

	@Override
	public void getCompositeForm(Composite formContainer) {
		comp = new CompMentor(formContainer, SWT.BORDER);

	}

	@Override
	public void reloadForm() {

		if (pjo != null) {
			comp.getTxtKode().setText(Util.nullToString(pjo.getCode()));
			comp.getTxtName().setText(Util.nullToString(pjo.getName()));
			comp.getTxtPhone().setText(Util.nullToString(pjo.getPhone()));
			comp.getTxtEmail().setText(Util.nullToString(pjo.getEmail()));
			comp.getTxtKursus().setText(Util.nullToString(pjo.getKursusdiampu()));
			comp.getCmbPendidikan().setText(Util.nullToString(pjo.getEducation()));
			comp.getTxtAddress().setText(Util.nullToString(pjo.getAddress()));
			Util.setDateTimeWithDate(pjo.getJoinDate(), comp.getTxtJoinDate());

		}
	}

	@Override
	public void doClearForm(SelectionEvent arg0) {
		pjo = new Mentor();
		reloadForm();
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void enabledControl(boolean otherfield, boolean key) {
		comp.getTxtKode().setEditable(otherfield);
		comp.getTxtName().setEditable(otherfield);
	}

	@Override
	public boolean doValidate() {
		if (pjo == null) {
			pjo = new Mentor();
		}
		pjo.setCode(comp.getTxtKode().getText());
		pjo.setName(comp.getTxtName().getText());
		pjo.setAddress(comp.getTxtAddress().getText());
		pjo.setPhone(comp.getTxtPhone().getText());
		pjo.setKursusdiampu(comp.getTxtKursus().getText());
		pjo.setJoinDate(Util.getDateSelection(comp.getTxtJoinDate()));
		pjo.setEducation(comp.getCmbPendidikan().getText());
		pjo.setEmail(comp.getTxtEmail().getText());

		if (Util.isEmpty(pjo.getCode())) {
			Util.showMsgBox("Kode harus dilengkapi!", 1, getShell());
			comp.getTxtKode().setFocus();
			return false;
		}

		if (Util.isEmpty(pjo.getName())) {
			Util.showMsgBox("Nama harus dilengkapi!", 1, getShell());
			comp.getTxtName().setFocus();
			return false;
		}

		if (Util.isEmpty(pjo.getAddress())) {
			Util.showMsgBox("Alamat harus dilengkapi!", 1, getShell());
			comp.getTxtAddress().setFocus();
			return false;
		}

		if (Util.isEmpty(pjo.getPhone())) {
			Util.showMsgBox("Phone harus dilengkapi!", 1, getShell());
			comp.getTxtPhone().setFocus();
			return false;
		}

		if (Util.isEmpty(pjo.getKursusdiampu())) {
			Util.showMsgBox("Kursus yang diampu harus dilengkapi!", 1,
					getShell());
			comp.getTxtKursus().setFocus();
			return false;
		}

		if (Util.isEmpty(pjo.getEducation())) {
			Util.showMsgBox("Pendidikan terakhir  harus dilengkapi!", 1,
					getShell());
			comp.getCmbPendidikan().setFocus();
			return false;
		}

		if (Util.isEmpty(pjo.getEmail())) {
			Util.showMsgBox("Email harus dilengkapi!", 1, getShell());
			comp.getTxtEmail().setFocus();
			return false;
		}

		return true;
	}

	@Override
	protected void setPojoWithFormField() throws Exception {
		/*
		 * if (pjo == null) { pjo = new Mentor(); }
		 * pjo.setCode(comp.getTxtKode().getText());
		 * pjo.setName(comp.getTxtName().getText());
		 * pjo.setAddress(comp.getTxtAddress().getText());
		 * pjo.setPhone(comp.getTxtPhone().getText());
		 * pjo.setKursusdiampu(comp.getTxtKursus().getText());
		 * pjo.setJoinDate(Util.getDateSelection(comp.getTxtJoinDate()));
		 * pjo.setEducation(comp.getCmbPendidikan().getText());
		 */
	}

	@Override
	protected void getPojoByFormKey(String key) throws Exception {
		try {
			pjo = (Mentor) AyukuDB.getPersist().readByPrimaryKey(Mentor.class,
					new Object[] { getFormKey() });
		} catch (Exception e) {

		}
		reloadForm();
	}

	@Override
	protected int doMainInsert() throws Exception {
		setPojoWithFormField();
		System.out.println("....Masuk insert ...");
		return AyukuDB.getPersist().insert(pjo);
	}

	@Override
	protected int doMainUpdate() throws Exception {
		setPojoWithFormField();
		System.out.println("....Masuk update ...");
		return AyukuDB.getPersist().update(pjo);
	}

	@Override
	protected int doMainDelete() throws Exception {
		return delete(pjo.getCode());
	}

	@Override
	protected int delete(String key) throws Exception {
		return AyukuDB.getPersist().executeUpdate(
				"delete from mentor where code = ?",
				new Object[] { pjo.getCode() });

	}

	@Override
	public void setFocusForAdd() {
		comp.getTxtKode().setFocus();
	}

	@Override
	public void setFocusForEdit() {
		comp.getTxtName().setFocus();
	}

}
